﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using CommDll;

namespace HospitalMedicineTraceability
{
    /// <summary>
    /// ClassName:frmDataShowSetup
    /// Version:1.0
    /// Date:2016-06-23
    /// Author:seanouyang
    /// </summary>
    /// <remarks>
    /// 本类是一WinForm 窗体,本窗体主要为实现数据显示设置
    /// </remarks>
    public partial class frmDataShowSetup : DevComponents.DotNetBar.OfficeForm
    {
        #region 1.自定义属性
        public DataGridView m_dgvMain = null;   // 信息类别
        public string m_strInfoKind = "";   // 信息类别
        public Boolean m_bIsSaveWidth = false;  // 是否保存显示宽度
        Dictionary<string, string> m_dictInfoName = new Dictionary<string, string>();
        Dictionary<string, string> m_dictInfoName0 = new Dictionary<string, string>();
        Dictionary<string, string> m_dictInfoWidth = new Dictionary<string, string>(); 
        #endregion

        public frmDataShowSetup()
        {
            InitializeComponent();
        }
        
        #region 2.自定义方法
        /// <summary>
        /// 初始化
        /// </summary>
        private void _init()
        {
            if (AppConfig.m_dictFieldName[m_strInfoKind].Equals("")) {
                string strTemp = "";
                string strTemp1 = "";
                for (int i = 0;i<m_dgvMain.Columns.Count; i++)
                {
                    strTemp += (strTemp.Equals("") ? "" : ",") + string.Format("{0}-{1}", m_dgvMain.Columns[i].Name, m_dgvMain.Columns[i].HeaderText);
                    strTemp1 += (strTemp1.Equals("") ? "" : ",") + string.Format("{0}", m_dgvMain.Columns[i].Name, m_dgvMain.Columns[i].HeaderText);
                }
                AppConfig.m_dictFieldName[m_strInfoKind] = strTemp;
                AppConfig.m_dictField[m_strInfoKind] = strTemp1;
            }
            // 01 取显示字段信息
            foreach (string str in AppConfig.m_dictFieldName[m_strInfoKind].Split(','))
            {
                m_dictInfoName.Add(str.Split('-')[0].Trim(), str.Split('-')[1].Trim());
                m_dictInfoName0.Add(str.Split('-')[1].Trim(), str.Split('-')[0].Trim());
            }
            string strInfo = "", strWidth = "";
            strWidth = AppConfig.m_dictField[m_strInfoKind];
            for (int i = 0; i < AppConfig.m_dictField[m_strInfoKind].Split(',').Length; i++)
            {
                strInfo = AppConfig.m_dictField[m_strInfoKind].Split(',')[i].Trim();
                if (AppConfig.m_dictFieldWidth[m_strInfoKind].Split(',').Length >= (i + 1))
                {
                    strWidth = AppConfig.m_dictFieldWidth[m_strInfoKind].Split(',')[i].Trim();
                }
                m_dictInfoWidth.Add(strInfo, strWidth);
            }
            // 02 显示内容
            int nWidth = 0;
            foreach (string str in m_dictInfoWidth.Keys)
            {
                try
                {
                    nWidth = FunctionComm._strToInt(m_dictInfoWidth[str]);
                    if (nWidth < 5)
                    {
                        lbShow0.Items.Add(m_dictInfoName[str]);
                    }
                    else
                    {
                        lbShow1.Items.Add(m_dictInfoName[str]);
                    }
                }
                catch { }
            }
            nWidth = 0;
            //
        }

        /// <summary>
        /// 保存信息
        /// </summary>
        private void _saveInfo()
        {
            // 01 取显示项
            string strField = "";
            string strFieldWidth = "";
            int nWidth = 0;
            foreach (string str in lbShow1.Items)
            {
                strField += (strField.Equals("") ? "" : ",") + m_dictInfoName0[str];
                nWidth = FunctionComm._strToInt(m_dictInfoWidth[m_dictInfoName0[str]]);
                nWidth = nWidth < 5 ? 80 : nWidth;// 隐藏时默认值80
                strFieldWidth += (strFieldWidth.Equals("") ? "" : ",") + nWidth.ToString();
            }
            if (strField.Equals(""))
            {
                CommFun._messageBoxShow("显示项不能为空！", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                // 02 取隐藏项
                foreach (string str in lbShow0.Items)
                {
                    strField += (strField.Equals("") ? "" : ",") + m_dictInfoName0[str];
                    strFieldWidth += (strFieldWidth.Equals("") ? "" : ",") + "0";
                }
                // 03 保存数据
                AppConfig.m_dictField[m_strInfoKind] = strField;
                AppConfig.m_dictFieldWidth[m_strInfoKind] = strFieldWidth;
                AppConfig._saveDgvTitle(m_strInfoKind);
                DialogResult = DialogResult.OK;
            }
        }

        private void moveUpListBox(ListBox ListBox1) //向上移动
        {
            //若不是第一行则上移
            if (ListBox1.SelectedIndex > 0)
            {
                int index = ListBox1.SelectedIndex;
                string temp = ListBox1.Items[index - 1].ToString();
                ListBox1.Items[index - 1] = ListBox1.SelectedItem.ToString(); ;
                ListBox1.Items[index] = temp;
                ListBox1.SelectedIndex = index - 1;
            }
        }

        private void moveDownListBox(ListBox ListBox1)  ///向下移动
        {
            //若不是第最后一行则下移
            if (ListBox1.SelectedIndex < ListBox1.Items.Count - 1)
            {
                int index = ListBox1.SelectedIndex;
                string temp = ListBox1.Items[index + 1].ToString();
                ListBox1.Items[index + 1] = ListBox1.SelectedItem.ToString(); ;
                ListBox1.Items[index] = temp;
                ListBox1.SelectedIndex = index + 1;
            }
        }
        
        #endregion

        #region 3.系统事件方法
        private void frmDataShowSetup_Shown(object sender, EventArgs e)
        {
            _init();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            _saveInfo();
        }

        private void btnDefault_Click(object sender, EventArgs e)
        {
            DialogResult result = CommFun._messageBoxShow("是否确定恢复默认值?", "系统提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (result == DialogResult.Yes)
            {
                AppConfig.m_dictField[m_strInfoKind] = AppConfig.m_dictField0[m_strInfoKind];
                AppConfig.m_dictFieldWidth[m_strInfoKind] = AppConfig.m_dictFieldWidth0[m_strInfoKind];
                AppConfig._saveDgvTitle(m_strInfoKind);
                DialogResult = DialogResult.OK;
            }
        }

        private void btnSaveWidth_Click(object sender, EventArgs e)
        {
            m_bIsSaveWidth = true;
            DialogResult = DialogResult.OK;
        }

        private void btnReturn_Click(object sender, EventArgs e)
        {
            this.Close();
        }              

        private void lbShow0_DrawItem(object sender, DrawItemEventArgs e)
        {
            // 重画控件，使行间距可以调整
            if (e.Index < 0)
            {
                return;
            }
            e.DrawBackground();
            e.DrawFocusRectangle();
            e.Graphics.DrawString(((ListBox)sender).Items[e.Index].ToString(), e.Font, new SolidBrush(e.ForeColor), e.Bounds.X, e.Bounds.Y + 3);  
        }
        
        private void lbShow0_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            // 增加显示项
            if (lbShow0.SelectedIndex > -1)
            {
                lbShow1.Items.Add(lbShow0.Items[lbShow0.SelectedIndex].ToString());
                lbShow0.Items.RemoveAt(lbShow0.SelectedIndex);
            }
        }

        private void lbShow1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            // 删除显示项
            if (lbShow1.SelectedIndex > -1)
            {
                lbShow0.Items.Add(lbShow1.Items[lbShow1.SelectedIndex].ToString());
                lbShow1.Items.RemoveAt(lbShow1.SelectedIndex);
            }
        }

        private void bindingNavigator1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
            switch (e.ClickedItem.Name)
            {
                case "tsbtnUp":
                    moveUpListBox(lbShow1);
                    break;
                case "tsbtnDown":
                    moveDownListBox(lbShow1);
                    break;
                case "tsbtnClear":
                    // 清除数据数据
                    foreach (string str in lbShow1.Items)
                    {
                        lbShow0.Items.Add(str);
                    }
                    lbShow1.Items.Clear();
                    break;
            }
        }
        #endregion        
    }
}